package com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyLine.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyLine.repository.HddcWyGeomorphysvylineNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyLine.repository.HddcWyGeomorphysvylineRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyLine.repository.entity.HddcWyGeomorphysvylineEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyLine.service.HddcWyGeomorphysvylineService;
import com.css.zfzx.sjcj.modules.hddcwyGeomorphySvyLine.viewobjects.HddcWyGeomorphysvylineQueryParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * @author lihelei
 * @date 2020-12-01
 */
@Service
public class HddcWyGeomorphysvylineServiceImpl implements HddcWyGeomorphysvylineService {

    @Autowired
    private HddcWyGeomorphysvylineRepository hddcWyGeomorphysvylineRepository;
    @Autowired
    private HddcWyGeomorphysvylineNativeRepository hddcWyGeomorphysvylineNativeRepository;

    @Override
    public JSONObject queryHddcWyGeomorphysvylines(HddcWyGeomorphysvylineQueryParams queryParams , int curPage,
                                                   int pageSize) {
        Page<HddcWyGeomorphysvylineEntity> hddcWyGeomorphysvylinePage =
                this.hddcWyGeomorphysvylineNativeRepository.queryHddcWyGeomorphysvylines(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeomorphysvylinePage);
        return jsonObject;
    }


    @Override
    public HddcWyGeomorphysvylineEntity getHddcWyGeomorphysvyline(String id) {
        HddcWyGeomorphysvylineEntity hddcWyGeomorphysvyline = this.hddcWyGeomorphysvylineRepository.findById(id).orElse(null);
        return hddcWyGeomorphysvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorphysvylineEntity saveHddcWyGeomorphysvyline(HddcWyGeomorphysvylineEntity hddcWyGeomorphysvyline) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeomorphysvyline.setId(uuid);
        hddcWyGeomorphysvyline.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorphysvyline.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcWyGeomorphysvyline.setIsValid("1");
        this.hddcWyGeomorphysvylineRepository.save(hddcWyGeomorphysvyline);
        return hddcWyGeomorphysvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeomorphysvylineEntity updateHddcWyGeomorphysvyline(HddcWyGeomorphysvylineEntity hddcWyGeomorphysvyline) {
        hddcWyGeomorphysvyline.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeomorphysvyline.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeomorphysvylineRepository.save(hddcWyGeomorphysvyline);
        return hddcWyGeomorphysvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeomorphysvylines(List<String> ids) {
        List<HddcWyGeomorphysvylineEntity> hddcWyGeomorphysvylineList = this.hddcWyGeomorphysvylineRepository.findAllById(ids);
        if (!PlatformObjectUtils.isEmpty(hddcWyGeomorphysvylineList) && hddcWyGeomorphysvylineList.size() > 0) {
            for (HddcWyGeomorphysvylineEntity hddcWyGeomorphysvyline : hddcWyGeomorphysvylineList) {
//                this.hddcWyGeomorphysvylineRepository.delete(hddcWyGeomorphysvyline);
                hddcWyGeomorphysvyline.setIsValid("0");
                hddcWyGeomorphysvyline.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcWyGeomorphysvyline.setUpdateTime(new Date());
                this.hddcWyGeomorphysvylineRepository.save(hddcWyGeomorphysvyline);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeomorphysvyline(HddcWyGeomorphysvylineQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeomorphysvylineNativeRepository.queryHddcWyGeomorphysvyline(queryParams);
        return bigInteger;
    }

}
